package com.beone.smart.smartBeauty.controller.webApi;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beone.smart.smartBeauty.dao.*;
import com.beone.smart.smartBeauty.enumeration.DictTypeEnum;
import com.beone.smart.smartBeauty.exception.GlobalException;
import com.beone.smart.smartBeauty.model.entity.*;
import com.beone.smart.smartBeauty.model.form.DictConfigCreateForm;
import com.beone.smart.smartBeauty.model.form.DictConfigUpdateForm;
import com.beone.smart.smartBeauty.model.form.DictValueCreateForm;
import com.beone.smart.smartBeauty.model.form.DictValueUpdateForm;
import com.beone.smart.smartBeauty.model.result.ResultEntity;
import com.beone.smart.smartBeauty.model.result.ResultTool;
import com.beone.smart.smartBeauty.utils.LocalTimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by bruce on 20/3/5 -- 16:27
 */
@Api(description = "后台管理接口")
@Slf4j
@RestController
@RequestMapping(value = "/manage")
public class ManageController {
    @Autowired
    private BeautyDeviceDao beautyDeviceDao;
    @Autowired
    private WeightRecordDao weightRecordDao;
    @Autowired
    private SleepInfoRecordDao sleepInfoRecordDao;
    @Autowired
    private SkinGatherDao skinGatherDao;
    @Autowired
    private DictionaryConfigDao dictionaryConfigDao;

    @ApiOperation(value = "智能美容设备", notes = "列表查询")
    @GetMapping(value = "/beautyDevice")
    public ResultEntity beautyDevice(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                     @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<BeautyDeviceDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BeautyDeviceDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(BeautyDeviceDO::getTitle, searchKey);
        }
        queryWrapper.orderByDesc(BeautyDeviceDO::getJoinDate);
        return ResultTool.ok(beautyDeviceDao.selectPage(page, queryWrapper));
    }

    @ApiOperation(value = "体重上报数据", notes = "列表查询")
    @GetMapping(value = "/weightRecord")
    public ResultEntity weightRecord(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                     @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<WeightRecordDo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<WeightRecordDo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(WeightRecordDo::getWeightRecord, searchKey);
        }
        queryWrapper.orderByDesc(WeightRecordDo::getRecordDate);
        return ResultTool.ok(weightRecordDao.selectPage(page, queryWrapper));
    }

    @ApiOperation(value = "睡眠上报数据", notes = "列表查询")
    @GetMapping(value = "/sleepRecord")
    public ResultEntity sleepRecord(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                    @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<SleepInfoRecordDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SleepInfoRecordDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(SleepInfoRecordDO::getScore, searchKey);
        }
        queryWrapper.orderByDesc(SleepInfoRecordDO::getRecordDate);
        return ResultTool.ok(sleepInfoRecordDao.selectPage(page, queryWrapper));
    }

    @ApiOperation(value = "肤质检测上报数据", notes = "列表查询")
    @GetMapping(value = "/skinGather")
    public ResultEntity skinGather(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                   @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<SkinGatherDO> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SkinGatherDO> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(SkinGatherDO::getFuse, searchKey)
                    .or().like(SkinGatherDO::getSeban, searchKey);
        }
        queryWrapper.orderByDesc(SkinGatherDO::getRecordDate);
        return ResultTool.ok(skinGatherDao.selectPage(page, queryWrapper));
    }

    /**
     * 数据字典接口 -目录list
     *
     * @param pageSize  分页大小
     * @param pageNum   页码
     * @param searchKey 搜索关键字--对应目录的title
     * @return 符合条件的目录列表
     */
    @ApiOperation(value = "字典-目录列表", notes = "列表查询")
    @GetMapping(value = "/dictMenuList")
    public ResultEntity dictMenuList(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                     @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<DictionaryConfigDo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DictionaryConfigDo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(DictionaryConfigDo::getMenuTitle, searchKey);
        }
        //检索type为目录的数据
        queryWrapper.eq(DictionaryConfigDo::getType, DictTypeEnum.MENU.getCode());
        queryWrapper.orderByDesc(DictionaryConfigDo::getConfigDate);
        return ResultTool.ok(dictionaryConfigDao.selectPage(page, queryWrapper));
    }

    @ApiOperation(value = "字典-目录新增")
    @PostMapping(value = "/dictMenu")
    public ResultEntity dictMenuCreate(@RequestBody DictConfigCreateForm dictConfigForm) {
        DictionaryConfigDo dictionaryConfigDo = new DictionaryConfigDo();
        BeanUtils.copyProperties(dictConfigForm, dictionaryConfigDo);
        dictionaryConfigDo.setConfigDate(LocalTimeUtil.getDateTime());
        dictionaryConfigDo.setType(DictTypeEnum.MENU.getCode());
        return ResultTool.ok(dictionaryConfigDao.insert(dictionaryConfigDo));
    }

    @ApiOperation(value = "字典-目录修改")
    @PutMapping(value = "/dictMenu")
    public ResultEntity dictMenuUpdate(@RequestBody DictConfigUpdateForm dictConfigForm) {
        DictionaryConfigDo dictionaryConfigDo = new DictionaryConfigDo();
        BeanUtils.copyProperties(dictConfigForm, dictionaryConfigDo);
        dictionaryConfigDo.setConfigDate(LocalTimeUtil.getDateTime());
        return ResultTool.ok(dictionaryConfigDao.updateById(dictionaryConfigDo));
    }

    @ApiOperation(value = "字典-目录删除")
    @DeleteMapping(value = "/dictMenu")
    public ResultEntity dictMenuDelete(@RequestParam String id) {
        DictionaryConfigDo dictionaryConfigDo = dictionaryConfigDao.selectById(id);
        if (dictionaryConfigDo == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "未找到对应id的目录数据，请检查id是否正确");
        }
        return ResultTool.ok(dictionaryConfigDao.deleteById(id));
    }

    @ApiOperation(value = "字典-值查询列表", notes = "列表查询，传入目录id，得到该目录下的值列表")
    @GetMapping(value = "/dictValueList")
    public ResultEntity dictValueList(@RequestParam(value = "pageSize", required = false, defaultValue = "10") long pageSize,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") long pageNum,
                                      @RequestParam(value = "menuId") String menuId,
                                      @RequestParam(value = "searchKey", required = false) String searchKey) {
        Page<DictionaryConfigDo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DictionaryConfigDo> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(searchKey)) {
            queryWrapper.like(DictionaryConfigDo::getDictKey, searchKey);
        }
        //检索type为值的数据
        queryWrapper.eq(DictionaryConfigDo::getType, DictTypeEnum.VALUE.getCode());
        queryWrapper.eq(DictionaryConfigDo::getMenuId, menuId);
        queryWrapper.orderByDesc(DictionaryConfigDo::getConfigDate);
        return ResultTool.ok(dictionaryConfigDao.selectPage(page, queryWrapper));
    }

    @ApiOperation(value = "字典-值新增", notes = "传入新增实体")
    @PostMapping(value = "/dictValue")
    public ResultEntity dictValueCreate(@RequestBody DictValueCreateForm dictValueForm) {
        String menuId = dictValueForm.getMenuId();
        if (StringUtils.isEmpty(menuId)) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "目录id为null，请传入正确的目录id");
        }
        DictionaryConfigDo menuDo = dictionaryConfigDao.selectById(menuId);
        if (menuDo == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "目录查询为null，请传入正确的目录id");
        }
        DictionaryConfigDo dictionaryConfigDo = new DictionaryConfigDo();
        BeanUtils.copyProperties(dictValueForm, dictionaryConfigDo);
        dictionaryConfigDo.setConfigDate(LocalTimeUtil.getDateTime());
        dictionaryConfigDo.setType(DictTypeEnum.VALUE.getCode());
        return ResultTool.ok(dictionaryConfigDao.insert(dictionaryConfigDo));
    }

    @ApiOperation(value = "字典-值修改", notes = "传入修改实体")
    @PutMapping(value = "/dictValue")
    public ResultEntity dictValueUpdate(@RequestBody DictValueUpdateForm dictValueForm) {
        DictionaryConfigDo menuDo = dictionaryConfigDao.selectById(dictValueForm.getId());
        if (menuDo == null) {
            throw new GlobalException(ResultTool.HttpResponseCode.NOT_FOUND, "查询为null，请传入正确的字典值id");
        }
        DictionaryConfigDo dictionaryConfigDo = new DictionaryConfigDo();
        BeanUtils.copyProperties(dictValueForm, dictionaryConfigDo);
        dictionaryConfigDo.setConfigDate(LocalTimeUtil.getDateTime());
        return ResultTool.ok(dictionaryConfigDao.updateById(dictionaryConfigDo));
    }

    @ApiOperation(value = "字典-值删除", notes = "传入id，删除字典值")
    @DeleteMapping(value = "/dictValue/{id}")
    public ResultEntity dictValueDelete(@PathVariable("id") String id) {
        return ResultTool.ok(dictionaryConfigDao.deleteById(id));
    }
}
